$('.select2').select2();
// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

function pic_replace(dom1,dom2){
var file = document.getElementById(dom1).files;
var reader = new FileReader();
reader.readAsDataURL(file[0]);
reader.onload = function(e){
  var Img = '<img class="img_sty" src="'+this.result+'" width="85px" height="100px"/>';
  $('.'+dom2).html(Img)
}
}
function GetQueryString(name){
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if(r != null) return unescape(r[2]);
  return null;
}

$(document).ready(function() {
    // $("#product").addClass("active");

    getData();
    function getData() {
        var index = GetQueryString('productId');
        $.ajax({
            type:"get",
            url:"http://114.116.72.220:9999/ProductDetailShow",
            async:true,
            data:{
                productId:index
            },
            success:function(data){
                DataView(data[0]);
            }
        });
    }

    function DataView(data){

        $("#new_productName").val(data.product_name);
        $("#new_productNo").val(data.product_no);
        $("#new_productBrand").val(data.product_brand);
        $("#new_productPrice").val(data.product_price);
        $("#new_reference_price").val(data.product_refer);
        $("#new_productType").val(data.product_model).trigger('change');
        $("#new_productGroup1").val(data.product_type[0]).trigger('change');
        $("#new_productGroup2").val(data.product_type[1]).trigger('change');
        $("#new_productClass").val(data.product_status).trigger('change');

        var img = `<img src="${data.product_thumbnail}"  class="img_sty" />`;
        $('.Thumbnail').html(img);
        img = `<img src="${data.product_detail}"  class="img_sty" />`;
        $('.detail_pic').html(img);

        for (var i=0;i<5;i++) {
            var Td = `<img src="${data.product_img[i]}"  class="img_sty" />`;
            $('.product_banner'+(i+1)).html(Td);
        }
    }

    $('#new_productGroup1').change(function () {
        var man = ['套西', '单西', '裤子', '长袖衬衣', '短袖衬衣', '马甲', '大衣', '风衣', '夹克', '中华立领', '中式西服'];
        var woman = ['套西', '单西', '裤子', '长袖衬衣', '短袖衬衣', '短裙', '大衣', '风衣', '连衣裙', '旗袍', '中式西服'];
        var child = ['套西', '单西', '裤子', '长袖衬衣', '短袖衬衣', '短裙', '大衣', '风衣'];
        var work = ['男士西装', '男士衬衣', '男士裤子', '男士大衣', '男士风衣', '女士西装', '女士衬衣', '女士短裙', '女士大衣', '女士风衣', '连衣裙'];
        var group2 = [];
        var group1 = $("#new_productGroup1").val();
        if (group1 == "男装") {
            group2 = man;
        }else if (group1 == "女装") {
            group2 = woman;
        } else if (group1 == "童装") {
            group2 = child;
        } else if (group1 == "职业装") {
            group2 = work;
        }

        $("#new_productGroup2").empty();
        for (var i = 0; i < group2.length; i++) {
            var text = `<option value='${group2[i]}'>${group2[i]}</option>`;
            $("#new_productGroup2").append(text);
        }
    });
    $("#new_productClass").change(function () {
        var product_class = $("#new_productClass").val();
        if (product_class == "2") {    //只限时商品有参考价格
            $("#new_reference_price").removeAttr("readonly");

        } else {
            $("#new_reference_price").attr("readonly","readonly");
            $("#new_reference_price").val("");
        }

        if (product_class == "1") {    //只普通商品有分类
            $("#group").show();

        } else {
            $("#group").hide();
        }
    });

    $('#submit_all').click(function () {
        var textOnoff = true;

        //判断必填信息
        if ($('input[name=productName]').val() == '' && textOnoff == true) {
            textOnoff = false;
            layer.msg('请填写商品名称');
            return;

        } else if ($('input[name=productPrice]').val() == '' && textOnoff == true) {
            textOnoff = false;
            layer.msg('请填写商品价格');
            return;

        } else if ($("#new_productClass").val()=='2' && textOnoff == true) {
            if ($('input[name=referPrice]').val() == '') {
                textOnoff = false;
                layer.msg('请填写市场参考价格');
                return;

            } else if (parseFloat($('input[name=productPrice]').val()) > parseFloat($('input[name=referPrice]').val())) {
                layer.msg('参考价格应大于商品价格');
                textOnoff = false;
                return;
            }

        }

        if ($('input[name=productBrand]').val() == '' && textOnoff == true) {
            textOnoff = false;
            layer.msg('请填写商品品牌');
            return;

        } else if ($('.file_img1').text() == '+' && textOnoff == true) {
            layer.msg('请添加商品缩略图');
            textOnoff = false;
            return;

        } else if ($('.detail_pic').text() == '+' && textOnoff == true) {
            layer.msg('请添加商品详情介绍图');
            textOnoff = false;
            return;

        } else if (textOnoff) {

            for (var i = 0; i < $('.pic_l').length; i++) {
                if ($('.pic_l').eq(i).text() == '+') {
                    layer.msg('请添加商品轮播图至5张');
                    textOnoff = false;
                    return;
                }
            }

        }

        if (textOnoff) {
            // var pic_num = 0;//轮播图数量
            // for (var i = 0; i < $('.pic_l').length; i++) {
            //     if ($('.pic_l').eq(i).find('img').length == 1) {
            //         pic_num += 1;
            //     }
            // }

            var formData = new FormData(document.getElementById('new_form'));
            var index = GetQueryString('productId');
            formData.append('productId', index);

            $.ajax({
                type: "POST",
                url: "http://114.116.72.220:9999/UpdateProduct/",
                async: true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('提交成功', {time: 500}, function () {
                            location.reload();
                        });
                    } else if (data[1] == 'name' || data[1] == 'do not add again') {
                        layer.msg('商品名称重复');
                    } else {
                        layer.msg('提交失败');
                    }
                }
            });
        }
    });
});